<template>
    <el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
        <el-form :model="form" :rules="rules" :disabled="mode==='show'" ref="dialogForm" label-width="100px" label-position="right">
            <el-form-item label="分组code" prop="groupCode">
                <el-input v-model="form.groupCode" placeholder="请输入分组code" clearable></el-input>
            </el-form-item>
            <el-form-item label="配置名称" prop="configName">
                <el-input v-model="form.configName" placeholder="请输入配置名称" clearable></el-input>
            </el-form-item>
            <el-form-item label="配置key" prop="configKey">
                <el-input v-model="form.configKey" placeholder="请输入配置key" clearable></el-input>
            </el-form-item>
            <el-form-item label="配置值" prop="configValue">
                <el-input v-model="form.configValue" placeholder="请输入配置值" clearable></el-input>
            </el-form-item>
            <el-form-item v-if="mode!=='add'" label="类型" prop="configType">
                <sc-select v-model="form.configType" dict="common_type" placeholder="请选择类型" style="width: 100%;" disabled></sc-select>
            </el-form-item>
			<el-form-item label="备注" prop="remark">
				<el-input v-model="form.remark" type="textarea" placeholder="请输入备注" clearable></el-input>
			</el-form-item>
        </el-form>
        <template #footer>
            <el-button @click="visible=false">取 消</el-button>
            <el-button v-if="mode!=='show'" type="primary" :loading="isSaving" @click="submit()">保 存</el-button>
        </template>
    </el-dialog>
</template>

<script>
    export default {
        emits: ['success', 'closed'],
        data() {
            return {
                // 表单元素控制
                mode: "add",
                titleMap: {
                    add: '新增',
                    edit: '编辑',
                    show: '查看'
                },
                visible: false,
                isSaving: false,
                // 表单数据
                form: {
                    id: null,
                    groupCode: null,
                    configName: null,
                    configKey: null,
                    configValue: null,
                    configType: null,
                    remark: null
                },
                // 表单验证规则
                rules: {
                    groupCode: [
                      {required: true, message: '请输入分组code', trigger: 'blur'}
                    ],
                    configName: [
                      {required: true, message: '请输入配置名称', trigger: 'blur'}
                    ],
                    configKey: [
                      {required: true, message: '请输入配置key', trigger: 'blur'}
                    ],
                    configValue: [
                      {required: true, message: '请输入配置值', trigger: 'blur'}
                    ],
                    configType: [
                      {required: true, message: '请输入类型', trigger: 'blur'}
                    ]
                }
            }
        },
        mounted() {
        },
        methods: {
            // 显示
            open(mode = 'add') {
                this.mode = mode;
                this.visible = true;
                return this;
            },
            // 表单注入数据
            setData(data) {
                this.form.id = data.id;
                this.form.groupCode = data.groupCode;
                this.form.configName = data.configName;
                this.form.configKey = data.configKey;
                this.form.configValue = data.configValue;
                this.form.configType = data.configType;
                this.form.remark = data.remark;
                this.form.status = data.status;
                this.form.createTime = data.createTime;
            },
            // 表单提交
            submit() {
                this.$refs.dialogForm.validate((valid) => {
                    if (valid) {
                        this.isSaving = true;
                        if (this.form.id) {
                            this.$API.system.config.edit.put(this.form).then(() => {
                                this.$emit('success');
                                this.visible = false;
                                this.$message.success("保存成功");
                            }).catch(() => {
                                this.isSaving = false;
                            });
                        } else {
                            this.$API.system.config.add.post(this.form).then(() => {
                                this.$emit('success');
                                this.visible = false;
                                this.$message.success("保存成功");
                            }).catch(() => {
                                this.isSaving = false;
                            });
                        }
                    }
                })
            }
        }
    }
</script>

<style>
</style>
